package li.com.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
import io.swagger.annotations.Api;
import li.com.ruiji.common.R;
import li.com.ruiji.entity.AddressBook;
import li.com.ruiji.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/addressBook")
@Slf4j
@Api("地址相关操作")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
//    查询地址
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }
//    添加地址
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook,HttpSession session){
        Long userId = (Long)session.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("地址添加成功");
    }
//    设置默认地址
    @PutMapping("/default")
    public R<String> updateDefault(@RequestBody AddressBook addressBook,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        List<AddressBook> list1=list.stream().map((item->{
            AddressBook addressBook1 = new AddressBook();
            item.setIsDefault(0);
            BeanUtils.copyProperties(item,addressBook1);
            return addressBook1;
        })).collect(Collectors.toList());
        addressBookService.updateBatchById(list1);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("地址设置默认成功");
    }
//    获取默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefault(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(queryWrapper);
        return R.success(one);
    }
//    修改地址时显示在页面上的内容
    @GetMapping("/{id}")
    public R<AddressBook> getAddress(@PathVariable Long id){
        AddressBook byId = addressBookService.getById(id);
        return R.success(byId);
    }
//    提交修改结果
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("地址修改成功");
    }
//    删除地址
    @DeleteMapping
    private R<String> delete(Long ids){
        addressBookService.removeById(ids);
        return R.success("地址删除成功");
    }
}
